package com.machine.filling.function.main.orderInfoFg;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.machine.filling.Test.TestCommon;
import com.machine.filling.db.dao.CardTempDao;
import com.machine.filling.db.dao.ClientInfoDao;
import com.machine.filling.db.dao.GunInfoDao;
import com.machine.filling.db.modle.ClientInfoModle;
import com.machine.filling.net.http.network.APIHttpManager;
import com.machine.filling.net.http.network.BaseHttpStringCallback;
import com.machine.filling.net.serialPort.send.CMD_OrderFueling;
import com.machine.filling.net.serialPort.send.CMD_OrderFuelingCancle;
import com.machine.filling.net.serialPort.util.SerialPortSendUtil;
import com.machine.filling.net.serialPort.util.SerialPortToolManager;
import com.machine.filling.net.ws.response.WsResPreOrderInfo;
import com.machine.filling.util.Common;
import com.machine.filling.util.HexUtils;
import com.machine.filling.util.LogUtil;
import com.machine.filling.util.SpUtils;

import android_serialport_api.SerialPort;
import okhttp3.Call;

/**
 * Created by Administrator on 2018/3/2 0002.
 */

public class OrderInfoFgPresenter implements OrderInfoFgContract.Presenter {
    private static final String TAG = "OrderInfoFgPresenter";
    private Context mContext;
    private OrderInfoFgContract.View mView;

    public OrderInfoFgPresenter(Context mContext, OrderInfoFgContract.View mView) {
        this.mContext = mContext;
        this.mView = mView;
    }

    @Override
    public void start() {

    }

    @Override
    public void distoryContext() {

    }

    /**
     * 获取预约订单信息
     */
    @Override
    public void getPreOrderInfo(String portNum) {
        String token = ClientInfoDao.getInstance().getModleByDeviceNum().getToken();
        String stationId = mView.getStationId();
        String gunId = mView.getGunId();
        String orderNum = mView.getPreOrderNum();

        APIHttpManager.getInstance().getPreOrderInfo(token, stationId, gunId, orderNum, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                WsResPreOrderInfo bean = new Gson().fromJson(result, WsResPreOrderInfo.class);
                mView.showPreOrderInfo(bean);
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                mView.showPreOrderInfoError(errorCode, errorMsg);
            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 发送 定量加油
     *
     * @param portNum
     * @param orderBean
     */
    @Override
    public void port_sendOrder_110A(String portNum, WsResPreOrderInfo orderBean) {
        CMD_OrderFueling orderFueling = new CMD_OrderFueling();
        orderFueling.setOrderMoney_7(HexUtils.str2ASCII(HexUtils.formatDouble(Double.parseDouble(orderBean.getAppPrice()), 2), 7));
        orderFueling.setYouhuiMoney_5(HexUtils.str2ASCII(HexUtils.formatDouble(Double.parseDouble("0"), 2), 5));
        orderFueling.setPingzhenghao_2(SpUtils.getInstance(mContext).getNextPingZhengHao());
        LogUtil.e("发送指令-->" + SerialPortSendUtil.getInstance().getCmd_StartOrderFueling110A(
                Common.MACHINE_VERSION,
                HexUtils.coverStrFromFirst(GunInfoDao.getInstance().queryBytPortNum(portNum).getGunNum(), 2),
                orderFueling.getHexStr()));



        SerialPortToolManager.getInstance().sendSerialPortCMD_DATA(portNum,
                SerialPortSendUtil.getInstance().getCmd_StartOrderFueling110A(
                        Common.MACHINE_VERSION,
                        HexUtils.coverStrFromFirst(GunInfoDao.getInstance().queryBytPortNum(portNum).getGunNum(), 2),
                        orderFueling.getHexStr()
                ));

    }

    /**
     * 取消定量加油
     *
     * @param portNum
     * @param orderBean
     */
    @Override
    public void port_sendCancleOrder_110B(String portNum, WsResPreOrderInfo orderBean) {
        Log.d(TAG, "port_sendCancleOrder_110B() returned: " + "取消定量");
        CMD_OrderFuelingCancle cancle = new CMD_OrderFuelingCancle();
        cancle.setOrderMoney_7(HexUtils.str2ASCII(HexUtils.formatDouble(Double.parseDouble(orderBean.getAppPrice()), 2), 7));
        cancle.setLiushuihao_2(SpUtils.getInstance(mContext).getNextPingZhengHao());

        String frameStr = SerialPortSendUtil.getInstance().getCmd_StartOrderFueling110B(
                Common.MACHINE_VERSION,
                HexUtils.coverStrFromFirst(GunInfoDao.getInstance().queryBytPortNum(portNum).getGunNum(), 2),
                cancle.getHexStr());
        LogUtil.e("port_sendCancleOrder_110B-->取消定量加油指令-->" + frameStr);
        SerialPortToolManager.getInstance().getSerialPortTool(portNum).sendSerialPort(frameStr);
    }

    /**
     * http解锁 订单
     *
     * @param portNum
     */
    @Override
    public void http_unLockOrder(final String portNum) {
        ClientInfoModle clientInfoModle = ClientInfoDao.getInstance().getModleByDeviceNum();
        String token = clientInfoModle.getToken();
        String machineId = clientInfoModle.getMachineId();
        APIHttpManager.getInstance().unLockOrder(token, machineId, mView.getPreOrderNum(), new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {

            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                http_unLockOrder(portNum);
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
